﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using JUtil;
using System.Reflection;

namespace DalGenerator
{
    public partial class SettingUI : Form
    {
        private Setting setting;

        public SettingUI()
        {
            InitializeComponent();

            this.setting = new Setting();

            this.Load += new EventHandler(LoadSetting);
        }

        public SettingUI(Setting setting)
        {
            InitializeComponent();

            this.setting = setting;

            this.Load += new EventHandler(LoadSetting);
        }

        void LoadSetting(object sender, EventArgs e)
        {
            InitializeOptions();

            InitGridView();
        }


        void InitGridView()
        {
            List<Dal> lst = new List<Dal>();

            lst.Add(new Dal("1", "2", "3"));
            lst.Add(new Dal("11", "22", "33"));
            lst.Add(new Dal("111", "222", "333"));

            DataTable dt = CreateTableByDalType<Dal>();

            //LoadDataRowFromDal<Dal>(dt, lst);

            gvDal.DataSource = dt;
        }

        public DataTable CreateTableByDalType<T>()
        {
            DataTable dt = new DataTable();

            DataColumn col = null;
            foreach (PropertyInfo pi in typeof(T).GetProperties())
            {
                col = new DataColumn(pi.Name, pi.PropertyType);
                dt.Columns.Add(col);
            }

            return dt;
        }

        public void LoadDataRowFromDal<T>(DataTable dt, T DalObject)
        {
            DataRow row = dt.NewRow();
            foreach (PropertyInfo pi in typeof(T).GetProperties())
            {
                object value = pi.GetValue(DalObject, null);

                row[pi.Name] = value;
            }
            dt.Rows.Add(row);
        }

        public void LoadDataRowFromDal<T>(DataTable dt, List<T> lstDalObject)
        {
            foreach (T DalObject in lstDalObject)
                LoadDataRowFromDal<T>(dt, DalObject);
        }

        void InitializeOptions()
        {
            cbDatabaseMode.DisplayMember = "TEXT";
            cbDatabaseMode.ValueMember = "VALUE";
            cbDatabaseMode.DataSource = GetDatabaseModeDataSource();
        }

        private DataTable GetDatabaseModeDataSource()
        {
            DataTable source = new DataTable();

            DataColumn col = null;
            DataRow row = null;

            col = new DataColumn("TEXT", typeof(string));
            source.Columns.Add(col);

            col = new DataColumn("VALUE", typeof(DatabaseMode));
            source.Columns.Add(col);

            foreach (DatabaseMode mode in Enum.GetValues(typeof(DatabaseMode)))
            {
                row = source.NewRow();
                row["TEXT"] = mode.ToString();
                row["VALUE"] = mode;
                source.Rows.Add(row);
            }

            return source;
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folderBrowser = new FolderBrowserDialog();

            folderBrowser.ShowDialog();

            tbExportDirectory.Text = folderBrowser.SelectedPath;
        }



    } // SettingUI

    public class Setting
    {
        public string Description;
        public string ExportDirectory;
        public string DatabaseMode;
        public string ConnectionString;

        List<string> Dal;
    }

    public class Dal
    {
        public Dal(string Namespace, string ClassName, string SelectSQL)
        {
            this.Namespace = Namespace;
            this.ClassName = ClassName;
            this.SelectSQL = SelectSQL;
        }

        public string Namespace {get;set;}
        public string ClassName { get; set; }
        public string SelectSQL { get; set; }
    }
}
